A Query to remove relationships that do not belong [closed]

Posted by Segfault on Stack Overflow See other posts from Stack Overflow or by Segfault
Published on 2012-06-20T17:39:57Z Indexed on 2012/07/02 15:16 UTC
Read the original article Hit count: 165

Filed under:
|
|

In a SQL Server 2008 R2 database, given this schema:

AgentsAccounts
_______________
AgentID int UNIQUE
AccountID


FinalAgents
___________
AgentID

I need to create a query that does this: For each AgentID 'final' in FinalAgents remove all of the OTHER AgentID's from AgentsAccounts that have the same AccountID as 'final'. So if the tables have these rows before the query:

AgentsAccounts
AgentID    AccountID
1          A
2          A
3          B
4          B

FinalAgents
1
3

then after the query the AgentsAccounts table will look like this:

AgentsAccounts
AgentID    AccountID
1          A
3          B

What T-SQL query will delete the correct rows without using a curosr?

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about query